<?php

namespace app\shop\controller\company;
use app\common\service\Result;
use app\shop\model\company\Station as StationModel;
/**
 * 员工管理
 */
class Station
{
    /**
     * 员工列表
     */
    public function list()
    {
        $data = input();
        $map = [
            ['app_id', '=', request()->app_id]
        ];
        $department_id = $data['department_id'] ?? 0;
        if ($department_id) {
            $map[] = ['department_id', '=', $department_id];
        }
        $lists = StationModel::where($map)->field('id,title,sort,department_id,trainer,file')->with('trainerUser')->order('sort desc')->select();
        return Result::success($lists);
    }

    public function save()
    {
        $data = input();
        $id = $data['id'] ?? 0;
        if ($id) {
            $model = StationModel::where('app_id', request()->app_id)->find($id);
            if (!$model) {
                return Result::error('数据不存在');
            } else {
                $model->save($data);
            }
        } else {
            unset($data['id']);
            $data['app_id'] = request()->app_id;
            $model = StationModel::create($data);
        }
        return Result::success();
    }

    public function delete()
    {
        $id = input('id');
        $model = StationModel::where('app_id', request()->app_id)->find($id);
        if ($model) {
            $model->delete();
            return Result::success();
        } else {
            return Result::error('数据不存在');
        }
    }
}